/*
 * JPanelWizardEspejoSelectElements.java
 *
 * Created on 29-ago-2011, 16:23:22
 */
package com.ufps.cuatro.vistas.asistente.espejo.simple.wizard;

import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JCheckBox;
import javax.swing.JOptionPane;

import org.jdesktop.application.Action;

import com.ufps.cuatro.CuatroApp;
import com.ufps.cuatro.controlador.render.UtilRender;
import com.ufps.cuatro.modelo.representacion.componente.Componente;
import com.ufps.cuatro.util.Bandera;
import com.ufps.cuatro.util.Utileria;
import com.ufps.cuatro.util.wizard.UtilWizard;
import com.ufps.cuatro.vistas.representacion.JDialogSeleccionComponente;

/**
 *
 * @author Carlos A. Torres C. 
 */
public class JPanelWizardEspejoSimpleSelectElements extends javax.swing.JPanel implements Observer {
    private static final long serialVersionUID = 3804509231290149123L;
    
    private ArrayList<Componente> objComponentes;

    /** Creates new form JPanelWizardEspejoSelectElements */
    public JPanelWizardEspejoSimpleSelectElements() {
        initComponents();
        objComponentes = new ArrayList<Componente>(); 
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListElementos = new javax.swing.JList();
        jLabel3 = new javax.swing.JLabel();
        jLabelInfoElemento = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton1 = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jButton2 = new javax.swing.JButton();
        jCheckBoxConfirmar = new javax.swing.JCheckBox();

        setName("Form"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(JPanelWizardEspejoSimpleSelectElements.class);
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jListElementos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jListElementos.setName("jListElementos"); // NOI18N
        jListElementos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jListElementosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jListElementos);

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabelInfoElemento.setText(resourceMap.getString("jLabelInfoElemento.text")); // NOI18N
        jLabelInfoElemento.setName("jLabelInfoElemento"); // NOI18N

        jLabel4.setBackground(resourceMap.getColor("jLabel4.background")); // NOI18N
        jLabel4.setIcon(resourceMap.getIcon("jLabel4.icon")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jLabel4.setName("jLabel4"); // NOI18N
        jLabel4.setOpaque(true);

        jToolBar1.setFloatable(false);
        jToolBar1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jToolBar1.setRollover(true);
        jToolBar1.setBorderPainted(false);
        jToolBar1.setName("jToolBar1"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(JPanelWizardEspejoSimpleSelectElements.class, this);
        jButton1.setAction(actionMap.get("addElemento")); // NOI18N
        jButton1.setFocusable(false);
        jButton1.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton1.setName("jButton1"); // NOI18N
        jButton1.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton1);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        jButton2.setAction(actionMap.get("delElemento")); // NOI18N
        jButton2.setFocusable(false);
        jButton2.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton2.setName("jButton2"); // NOI18N
        jButton2.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(jButton2);

        jCheckBoxConfirmar.setAction(actionMap.get("CompletarPaso")); // NOI18N
        jCheckBoxConfirmar.setFont(resourceMap.getFont("jCheckBoxConfirmar.font")); // NOI18N
        jCheckBoxConfirmar.setText(resourceMap.getString("jCheckBoxConfirmar.text")); // NOI18N
        jCheckBoxConfirmar.setName("jCheckBoxConfirmar"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 665, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                                .addGap(8, 8, 8)
                                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(jCheckBoxConfirmar, javax.swing.GroupLayout.DEFAULT_SIZE, 351, Short.MAX_VALUE)))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabelInfoElemento, javax.swing.GroupLayout.DEFAULT_SIZE, 612, Short.MAX_VALUE)
                        .addGap(63, 63, 63))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 56, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 219, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelInfoElemento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBoxConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

private void jListElementosValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jListElementosValueChanged
        if(!this.jListElementos.isSelectionEmpty()){
            Componente obj = (Componente) this.jListElementos.getSelectedValue();            
            String strDef = "<html><b>" + obj.getName() + "</b></html>";    
            this.jLabelInfoElemento.setText(strDef);
        }
}//GEN-LAST:event_jListElementosValueChanged

    @Action
    public void addElemento() {        
        int[] tipoComponente = {Componente.COMPONENTE_DISTRIBUIDOR_GENERADOR};        
        JDialogSeleccionComponente objJDialogSeleccionComponente = new JDialogSeleccionComponente(this, JDialogSeleccionComponente.SELECCION_COLECCION_OBJECT, tipoComponente);
        objJDialogSeleccionComponente.setLocationRelativeTo(UtilWizard.objWizard.getDialog());                
        CuatroApp.getApplication().show(objJDialogSeleccionComponente);  
    }

    @Action
    public void delElemento() {
        if(this.objComponentes.isEmpty()){
            Utileria.mostrarMensaje(this, "Debe Agregar Componentes del Modelo Analizado.", "", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(this.jListElementos.isSelectionEmpty()){
            Utileria.mostrarMensaje(this, "Debe Seleccionar un Componente.", "", JOptionPane.WARNING_MESSAGE);
            return;
        }
        if(!Utileria.confirmarOrden(this, "¿Desea Eliminar el Componente?", ""))return;
        int i = this.jListElementos.getSelectedIndex();
        this.objComponentes.remove(i);
        UtilRender.helperRenderJList(jListElementos, objComponentes, 0);
        jLabelInfoElemento.setText("");
        if(objComponentes.isEmpty()){//Control del caso reportado en el cual se elimina el unico componente agregado.
            if(jCheckBoxConfirmar.isSelected()){                                  
                jCheckBoxConfirmar.doClick();                
            }
        }
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JCheckBox jCheckBoxConfirmar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabelInfoElemento;
    private javax.swing.JList jListElementos;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    // End of variables declaration//GEN-END:variables

    public void update(Observable o, Object arg) {
        if(o instanceof Bandera){            
            Bandera objBandera = (Bandera) o;
            if(objBandera.getEstado() == Bandera.SELECCION_COMPONENTE_DISTRIBUIDOR){
                if(!objComponentes.contains((Componente) objBandera.getObj())){
                    objComponentes.add((Componente) objBandera.getObj());		
                    UtilRender.helperRenderJList(jListElementos, objComponentes, 0);
                    jLabelInfoElemento.setText("");
                }
                else{
                    UtilRender.msgAdvertencia("No puede ingresar Componente que han sido incluidos Previamente.");
                }
            }           
        }        
    }

    public ArrayList<Componente> getObjComponentes() {
        return objComponentes;
    }

    public JCheckBox getjCheckBoxConfirmar() {
        return jCheckBoxConfirmar;
    }

    @Action
    public void addCDG() {
    }

    @Action
    public void addCDIM() {        
    }   
    
    public void addCheckBoxActionListener(ActionListener l) {
        jCheckBoxConfirmar.addActionListener(l);
    }
    
    public boolean isCheckBoxSelected() {
        return jCheckBoxConfirmar.isSelected();
    }
    
    public boolean isListEmpty(){
        return this.objComponentes.isEmpty();
    }

    @Action
    public void CompletarPaso() {
    }
}
